package site.wanjiahao.handler.server;

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import site.wanjiahao.handler.client.ListGroupsResponseMessageHandler;
import site.wanjiahao.message.request.ListGroupsRequestMessage;
import site.wanjiahao.message.response.ListGroupsResponseMessage;
import site.wanjiahao.session.SessionFactory;

import java.util.Set;

@ChannelHandler.Sharable
public class ListGroupsRequestMessageHandler extends SimpleChannelInboundHandler<ListGroupsRequestMessage> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, ListGroupsRequestMessage msg) throws Exception {
        Set<String> groups = SessionFactory.getGroupSession().listGroup();
        ListGroupsResponseMessage listGroupsResponseMessage = new ListGroupsResponseMessage();
        listGroupsResponseMessage.setGroups(groups);
        ctx.writeAndFlush(listGroupsResponseMessage);
    }
}
